<?php

$evo[ "best" ] = array();
$evo[ "worst" ] = array();
$pointsFirstMonth = 0;
$datetimePointsFirstMonth = null;
$licensee_id = null;
foreach($licensee_point_list as $licensee_point){
    if($licensee_id != $licensee_point['licensee_id']){
        if($pointsFirstMonth <= 0){
            $pointsFirstMonth = $licensee_point['nb_points_fftt'];
            $datetimePointsFirstMonth = $licensee_point['datetime_points'];
        }else{
            $var_nb_points = ($licensee_point['datetime_points'] > $datetimePointsFirstMonth ? $licensee_point['nb_points_fftt'] -  $pointsFirstMonth : $pointsFirstMonth - $licensee_point['nb_points_fftt']);
            $teamId = $licensee_point['Licensee']['Team']["id"];
            if($var_nb_points > 0){
                if(empty($evo[ "best" ][$teamId]) || $var_nb_points > $evo[ "best" ][$teamId]['points']){
                    $evo[ "best" ][$teamId] = array("name" => ($licensee_point['Licensee']['lastname'] . " " . $licensee_point['Licensee']['firstname']),
                                                    "slug" => $licensee_point['Licensee']['slug'],
                                                    "points" => $var_nb_points,
                                                    "team" => $licensee_point['Licensee']['Team']["name"] . " :");
                }
            }else{
                if(empty($evo[ "worst" ][$teamId]) || $var_nb_points < $evo[ "worst" ][$teamId]['points']){
                    $evo[ "worst" ][$teamId] = array("name" => ($licensee_point['Licensee']['lastname'] . " " . $licensee_point['Licensee']['firstname']),
                                                     "slug" => $licensee_point['Licensee']['slug'],
                                                     "points" => $var_nb_points,
                                                     "team" => $licensee_point['Licensee']['Team']["name"] . " :");
                }
            }
            $licensee_id = $licensee_point['licensee_id'];
            $pointsFirstMonth = 0;
            $datetimePointsFirstMonth = null;
        }
    }
}

?>

 <div id="best-evolution" class="evolution">
    <h2 class="text-shadowed"><span>Les meilleures progressions du mois</span></h2>
        <ul>
          <?php foreach($evo[ "best" ] as $evoBest){
            echo "<li><span class='team'>" . $evoBest["team"] . "</span><a href='" . url_for(array('module' => 'member', 'action' => 'index', 'name_slug' =>  $evoBest["slug"])) . "'>" . $evoBest["name"] . "</a><span class='points'>+" . $evoBest["points"] . "</span></li>";
          }?>
        </ul>
 </div>

 <div id="worst-evolution" class="evolution">
    <h2 class="text-shadowed"><span>Les régressions du mois</span></h2>
        <ul>
            <?php foreach($evo[ "worst" ] as $evoWorst){
                echo "<li><span class='team'>" . $evoWorst["team"] . "</span><a href='" . url_for(array('module' => 'member', 'action' => 'index', 'name_slug' => $evoWorst["slug"])) . "'>" . $evoWorst["name"] . "</a><span class='points'>" . $evoWorst["points"] . "</span></li>";
             }?>
        </ul>
 </div>